package test;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.util.HashMap;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.document.NumericField;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.LockObtainFailedException;
import org.apache.lucene.util.Version;

public class Spatial_Indexer {

	/**
	 * @param args
	 * dirPath specifies the path of the directory that contains all the text files to be indexed.
	 * indexPath specifies the path of the directory that you need to put your index files in.
	 * LocFile is the path of the file that contains all the location information.
	 */

	/*static String dirPath = "D:\\Program Files\\eclipse\\workspace\\cs6714\\sampleData1";
	static String indexPath = "D:\\Program Files\\eclipse\\workspace\\cs6714\\indexPath";
	static String LocFile = "D:\\Program Files\\eclipse\\workspace\\cs6714\\sampleLoc1.txt";*/

	public void build(String dirPath, String indexPath, String LocFile){
		
		File FILE_DIR  = new File(dirPath);
		File INDEX_DIR = new File(indexPath);

		Analyzer a = new StandardAnalyzer(Version.LUCENE_36);
		IndexWriter writer;
		try {
			writer = new IndexWriter(
					FSDirectory.open(INDEX_DIR), 
					new IndexWriterConfig(Version.LUCENE_36, a));
			File[] textFiles  = FILE_DIR.listFiles();
			HashMap<String, Coordinate> hm = this.readLoc(LocFile);
			NumericField x = new NumericField("x",Store.YES,true);
			NumericField y = new NumericField("y",Store.YES,true);

			//Add documents to the index
			for(int i = 0; i < textFiles.length; i++){
				if(textFiles[i].isFile() && textFiles[i].getName().endsWith(".txt")){
					Reader fileReader = new FileReader(textFiles[i]);
					String name = textFiles[i].getName();
					Document doc = new Document();
					x.setFloatValue(hm.get(name).getX());
					y.setFloatValue(hm.get(name).getY());
					doc.add(new Field("content", fileReader, Field.TermVector.YES));
					doc.add(new Field("name",textFiles[i].getName(), Field.Store.YES, Field.Index.ANALYZED));
					doc.add(x);
					doc.add(y);
					writer.addDocument(doc);
				}
			}
			writer.close();
		} catch (CorruptIndexException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (LockObtainFailedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
	}

	public HashMap<String, Coordinate> readLoc(String LocFile){
		HashMap<String, Coordinate> hm = new HashMap<String, Coordinate>();

		try {
			BufferedReader bf = new BufferedReader(new FileReader(LocFile));
			String text;  
			try {
				while((text = bf.readLine()) != null){
					String[] in = text.split(" ");
					float x = Float.parseFloat(in[1]);
					float y = Float.parseFloat(in[2]);
					Coordinate co = new Coordinate(x, y);
					String key = in[0];
					hm.put(key, co);
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return hm;
	}

	public static void main(String[] args) {
		Spatial_Indexer si = new Spatial_Indexer();
		String dirPath = args[0];
		String indexPath = args[1];
		String LocFile = args[2];
		si.build(dirPath, indexPath, LocFile);
	}
}
